package com.springboot.springbootweb.controller;

import com.springboot.springbootweb.Async.AsyncText;
import com.springboot.springbootweb.Bean.Person;
import com.springboot.springbootweb.Bean.Person2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

/**
 *
 */
@Controller
public class HelloController {

    @Autowired
    AsyncText async;

    @RequestMapping("/hello")
    @ResponseBody
    String hello() {
        async.asyncMethodWithVoidReturnType();
        System.out.println("************************");
        return "hello world";

    }

    @Autowired
    private Person person;

    @RequestMapping("/hello1")
    @ResponseBody
    Person hello1() throws InterruptedException, ExecutionException {
        Future<String> future = async.asyncMethodWithReturnType();
        while (true) {  ///这里使用了循环判断，等待获取结果信息
            if (future.isDone()) {  //判断是否执行完毕
                System.out.println("Result from asynchronous process - " + future.get());
                break;
            }
            System.out.println("Continue doing something else. ");
            Thread.sleep(1000);
        }
        System.out.println("111111111111111111111111");
        return person;

    }

    @Autowired
    private Person2 person2;

    @RequestMapping("/hello2")
    @ResponseBody
    Person2 hello2() {

        return person2;

    }
}
